using System.Web.UI.WebControls;
using System.Web.UI;
using System;
using System.Web;
using System.Drawing;
namespace BlogControl
{
    public static class AddNewPost
    {
        internal static void RenderNewPostForm(Blog control,HtmlTextWriter writer)
        {
            if (control.AddPostCSS != null)
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Class, control.AddPostCSS);
            }
            writer.AddStyleAttribute(HtmlTextWriterStyle.Color, ColorTranslator.ToHtml(control.AddPostTextColor));
            writer.RenderBeginTag(HtmlTextWriterTag.Table);

            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.Write(control.AddPostWriterText+ ":");
            writer.RenderEndTag();
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            ((TextBox)control.Controls[control.newPostWriterIndex]).RenderControl(writer);
            writer.RenderEndTag();
            writer.RenderEndTag();


            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.Write(control.AddPostSubjectText);
            writer.RenderEndTag();
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            ((TextBox)control.Controls[control.newPostTitleIndex]).RenderControl(writer);
            writer.RenderEndTag();
            writer.RenderEndTag();
            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.Write(control.AddPostBodyText);

            writer.RenderEndTag();
            writer.RenderBeginTag(HtmlTextWriterTag.Td);

            ((FreeTextBoxControls.FreeTextBox)control.Controls[control.newPostBodyIndex]).RenderControl(writer);
            writer.RenderEndTag();
            writer.RenderEndTag();

            writer.RenderBeginTag(HtmlTextWriterTag.Tr);

            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            ((Button)control.Controls[control.newPostCancelIndex]).RenderControl(writer);

            writer.RenderEndTag();
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
           
            ((Button)control.Controls[control.newPostSubmitIndex]).RenderControl(writer);
            writer.RenderEndTag();
            writer.RenderEndTag();

            writer.RenderEndTag();
        }

        internal static void SaveNewPost(Blog control, HtmlTextWriter writer)
        {

            string title = ((TextBox)control.Controls[control.newPostTitleIndex]).Text;
            string body = ((FreeTextBoxControls.FreeTextBox)control.Controls[control.newPostBodyIndex]).Text;
            string username = ((TextBox)control.Controls[control.newPostWriterIndex]).Text;
            string filename = FilesManager.AddPost(DateTime.Now, title, body, username, control.Directory);
            PostArguments pa = new PostArguments(title, username, body, filename);
            control.TriggerPostAdded(pa);
            writer.AddStyleAttribute(HtmlTextWriterStyle.Color, ColorTranslator.ToHtml(control.AddPostTextColor));
            writer.RenderBeginTag(HtmlTextWriterTag.Span);
            writer.Write(control.AddPostSuccessText);
            writer.RenderEndTag();
            writer.WriteBreak();
            Button b = (Button)control.Controls[control.newPostCancelIndex];
            b.Text = control.AddPostBackButtonText;
            writer.RenderBeginTag(HtmlTextWriterTag.Span);
            b.RenderControl(writer);
            writer.RenderEndTag();
            JavascriptManager.WriteCurrentAction(writer);
        }
    }
}